﻿var pageList={
	submit : function(){

		var $this, title, data;
		var msg = "검색조건 -> ";
		var query = "";
		$(".searchQueryTb", ".searchForm").find(":input.query")
			.each(function(i) {
				$this = $(this);
				title = $this.attr("title");
				if($this.is("select")){
					data = $this.find("option:selected").text();
				}else{					
					data = $this.val();
				}
				if(title && data){
					if(query) query += ", ";
					query += title+" : "+data;
				}
		});
		
		$(".searchQueryTb", ".searchForm").find("[name=logMsg]").val(msg+query);
		return true;
	},
	
	init : function(){
		var orderBy = $("[name=orderBy]").val();
		if(!orderBy) return true;

		var orderItemArray = orderBy.split(",");

		var $thead = $("thead", ".searchDataList");
		$.each(orderItemArray, function(i, val){
			var orderItem = val.split(" ");
			$thead.find("th.order[filed="+orderItem[0]+"]").addClass(orderItem[1]);
		});

		return true;
	},

	pageMove : function(){
		var pageNum = $(this).attr("pageNum");
		if(pageNum)	$(this).closest(".searchForm").find("[name=pageNum]").val(pageNum).end().submit();
		return false;
	},

	pageOrderby : function(){
		var $this = $(this);
		if($this.hasClass("DESC")){
			$this.removeClass("DESC").addClass("ASC");
		}else {
			$this.removeClass("ASC").addClass("DESC");
		};

		/**
		 * <th/> 클릭 시 order by 에 쿼리 넣어놓기
		 */
		var orderBy = "";
		$(this).closest("thead").find("th.order").each(function(i){
			var filed = $(this).attr("filed");
			if($(this).hasClass("DESC")){
				if(orderBy) orderBy+=",";
				orderBy += filed + " DESC";
			}else if($(this).hasClass("ASC")){
				if(orderBy) orderBy+=",";
				orderBy += filed + " ASC";
			}
		});

		$this.closest(".searchForm").find("[name=orderBy]").val(orderBy).end().submit();
		return false;
	}

};

$(function(){
	$("button[type=submit]",".searchForm").click(function(){
		$(".searchQueryTb", ".searchForm")
			.find("[name=pageNum]").val("").end()
			.find("[name=orderBy]").val("");
		
	});
	pageList.init();
	$("a", ".paginate").click(pageList.pageMove);
	$("select[name=pageCut]").change(function(){
		$(this).closest(".searchForm").submit();
	});
	$("th.order", ".searchDataList").click(pageList.pageOrderby);
});
